import router from '@/router'
import store from '@/store'
router.beforeEach(async(to, from, next) => {
  // 获取token
  const token = store.getters.token
  // 判断是否有token

  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      // 有token时判断有没有用户信息，如果没有就获取用户信息
      if (!store.getters.userInfo) {
        const res = await store.dispatch('user/getUserInfo')
        console.log(res)
      }
      next()
    }
    // 没有token
  } else {
    // 判断是否在白名单中
    const writerList = ['/login', '/404']
    if (writerList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
